package algorithm.offer;

import org.junit.Test;

import java.util.Stack;

public class Offer58StringWord {

  /**
   * 倒转 字符串
   * 空间复杂度
   * 时间复杂度
   * <p>
   * 除去多余空格
   */
  @Test
  public void test() {

    String src = " i love  you!! ";
//    src = " i  ";

    int total = src.length() - 1;

    int start = 0;
    int end = src.length() - 1;
    StringBuilder stringBuffer = new StringBuilder();
    //双指针
    while (true) {
      //找字母
      while (end >= 0 && ' ' == src.charAt(end)) {
        end--;
      }
      if (end < 0) break;
      start = end;
      //找空字符串
      while (start >= 0 && ' ' != src.charAt(start)) {
        start--;
      }
      //打印
      String substring = src.substring(start + 1, end + 1);
      System.out.println(substring);
      stringBuffer.append(substring).append(" ");

      end = start;
    }

    Stack<Integer> stack = new Stack<>();
    System.out.println(stringBuffer.toString());

  }

}
